﻿ using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Threading;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.IO;
public partial class Admin_adminResource :basePage
{
    ResourceBll rBLL = new ResourceBll();
    StringBuilder str = new StringBuilder();
    resourceClassBll rCBll = new resourceClassBll();
    productResourceBll prBll = new productResourceBll();
    private static readonly object objSync = new object();

    int pageSize = 10;
    public string input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=1'/>";


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["state"]!=null)
            {
                string state = Request.QueryString["state"].ToString();
                int resourceId=Convert.ToInt32(Request.QueryString["resourceID"].ToString());
                switch (state.Trim())
                {
                    case "Insert":
                        Insert();
                        break;
                    case "del":
                        Delete(resourceId);
                        break;
                    case "edit":
                        Edit(resourceId);
                        break;
                    case "update":
                        update(resourceId);
                        break;
                    case "delTb":
                        delTb(resourceId);
                        break;
                    case "MoveAll":
                        moveUpdate(Convert.ToInt32(Request.QueryString["resourceClassId"]));
                        break;
                    default:
                        break;
                }
            }
           BindResource();
        }

    }
    #region 解除关联---郑同飞
    public void delTb(int resourceId) 
    {
        Resource resource=rBLL.GetModel(resourceId);
        IList<productResource> prList = prBll.GetModelList(0);
        lock(objSync)
        {
            foreach (productResource prModel in prList )
            {   
                if (prModel.ResourceID.Contains("|"))
                {
                    string newResourceID = "";
                    IList<string> strResourceId = prModel.ResourceID.Split('|');
                    if(!strResourceId.Contains(resourceId.ToString()))
                    {
                        continue;
                    }
                    foreach (string rid in strResourceId)
                    {
                        if (resourceId != Convert.ToInt32(rid))
                        {
                            newResourceID += rid + "|";
                        }
                    }
                    if (newResourceID!="")
                    {
                        newResourceID = newResourceID.Remove(newResourceID.Trim().Length-1,1);
                        prModel.ResourceID = newResourceID;
                        if (prBll.Update(prModel)) 
                        {

                        }
                    }
                }                
                if (resourceId==resource.ResourceID)
                {
                    prModel.ResourceID = "";
                    if (prBll.Delete(resourceId.ToString()))
                    {

                    }
                }           
            }
        }
        Response.Write("解除成功!");
        Response.End();
    }
    #endregion

    #region 更新---郑同飞
    public void update(int resourceID) 
    {
        Resource resource = new Resource();
        resource.ResourceClassID = Convert.ToInt32(Request.Form["resourceClassID"]);
        resource.ResourceName = Request.Form["resourceName"].ToString();
        resource.ResourceURL = Request.Form["resourceURL"].ToString();
        resource.ResourceID = resourceID;
        resource.resourceDescription = Request.Form["description"].ToString();
        if (resource.ResourceName != "" && resource.ResourceURL != "" && resource.ResourceClassID != 0)
        {
            if (rBLL.Update(resource))
            {
                        
            }
        }
        else 
        {
            Response.Write("2");
            Response.End();           
        }
        
    }
    #endregion

    #region  编辑---郑同飞
    public void Edit(int resourceID) 
    {
        Resource resource = new Resource();
        resource = rBLL.GetModel(resourceID);
        this.resourceName_txt.Value = resource.ResourceName;
        ViewState["resourceClassID"] = resource.ResourceClassID;
        this.resource_hid.Value = resource.ResourceID.ToString();
        this.content.Value = resource.ResourceURL;
        this.txt_description.Value = resource.resourceDescription;
    }
    #endregion

    #region 删除---郑同飞
    public void Delete(int resourceID) 
    {
        bool bo = false;        
        IList<productResource> pRList = prBll.GetModelList(0);
        foreach (productResource pr in pRList)
        {
            if (bo)
            {
                break;
            }
            if (pr.ResourceID.Contains("|"))
            {
                string[] resourceIds = pr.ResourceID.Split('|');
                foreach (string rID in resourceIds)
                {
                    if (resourceID == Convert.ToInt32(rID))
                    {
                        bo = true;
                        break;
                    }
                }
            }
            else if(pr.ResourceID.Length>0)
            {
                if (Convert.ToInt32(pr.ResourceID) == resourceID)
                    bo = true;
            }
        }
        if (!bo)
        {
            string path=rBLL.GetModel(resourceID).ResourceURL; 
            if (rBLL.Delete(resourceID))
            {
                if (File.Exists(Server.MapPath("~") + path))
                File.Delete(Server.MapPath("~")+ path);                           
            }
        }
        else 
        {
            Response.Write("1");
            Response.End();
        }
        
    }
    #endregion

    #region 绑定---郑同飞
    public void BindResource() 
    {
        if (Request.QueryString["CurrentIndex"] != null)
        {
            _currentIndex = Convert.ToInt32(Request.QueryString["CurrentIndex"]);
        }
        if (Request.QueryString["ClassId"]!=null)
        {
            _classid = Convert.ToInt32(Request.QueryString["ClassId"]);
        }
        if (Request.QueryString["key"]!=null)
        {
            _key =Server.HtmlDecode(Request.QueryString["key"].ToString());           
        }
        DataTable dt = rBLL.pageingForResource(_currentIndex, pageSize, _classid, _key);
        if (dt.Rows.Count==0&&_currentIndex>1)
        {
            --_currentIndex;
            dt = rBLL.pageingForResource(_currentIndex,pageSize,_classid,_key);
        }
        input = "<input id='HiddenUrl' type='hidden' value='&CurrentIndex=" + _currentIndex + "&ClassId=" + _classid + "&key=" + _key + "' />";
        this.Repeater1.DataSource = dt;
        this.Repeater1.DataBind();
    }
    #endregion

    #region 添加---郑同飞
    public void Insert() 
    {
        Resource r = new Resource();
        r.ResourceClassID = Convert.ToInt32(Request.Form["resourceClassID"].ToString());
        r.ResourceName = Request.Form["resourceName"].ToString();
        r.ResourceURL = Request.Form["resourceURL"].ToString();      
        if (r.ResourceClassID != 0 && r.ResourceName != "" && r.ResourceURL != "")
        {
            if (rBLL.Insert(r))
            {
               
            }
        }
        else 
        {
            Response.Write("2");
            Response.End();
           
        }
        
    }
    #endregion

    #region 自定义下拉框---郑同飞
    /// <summary>
    /// 自定义下拉框---郑同飞
    /// </summary>
    /// <param name="name">空间的id</param>
    /// <returns></returns>
    public string resourceClassDp(string name)
    {
        str.Remove(0,str.Length);
        IList<resourceClass> resourceClassList;
        if (ViewState["resourceClass"] != null)
        {
            resourceClassList = ViewState["resourceClass"] as IList<resourceClass>;
        }
        else 
        {
            resourceClassList = rCBll.GetModel();
            ViewState["resourceClass"] = resourceClassList;
        }                
        str.Append("<select name=\""+name+"\" id=\"" + name + "\" style='width:120px;'>");
        str.Append("<option value=\"0\" selected=\"true\">请选择资源类别</option>");
        foreach (resourceClass rc in resourceClassList)
        {
            if (Convert.ToInt32(ViewState["resourceClassID"]) == rc.ResourceClassID && name == "resourceClassDp")
            {
                str.Append("<option value='" + rc.ResourceClassID + "' selected='selected'>" + rc.ResourceClassName + "</option>");
            }
            else
                str.Append("<option value='" + rc.ResourceClassID + "'>" + rc.ResourceClassName + "</option>"); 
        }
        str.Append("</select>");
        return str.ToString();
    }

    #endregion

    #region 根据资源类别id得到资源类别名称---郑同飞
    public resourceClass getResourceClassByResourceClassID(int resourceClassID)
    {
        resourceClass rcModel = new resourceClass();
        rcModel = rCBll.GetModel(resourceClassID);
        return rcModel;
    }
    #endregion

    #region 分页按钮---郑同飞
    public void htmlPaging() 
    {
        string str = null;
        int count = 0;
        count = rBLL.getRecordCount(_classid, _key);
        int pcount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        str = "页次：" + _currentIndex + "/" + pcount + "  每页" + pageSize + " 总收录数据" + count + "条";
        str += HtmlControls.pagIng(_currentIndex, pcount, "adminResource.aspx?ClassId=" + _classid + "&key=" +Server.UrlEncode(_key) + "&CurrentIndex", "pageaa", "pagecurrent");
        Response.Write(str);

    }
    #endregion

    #region 状态维护属性---郑同飞
    public int _currentIndex = 1;
    public int _classid = 0;
    public string _key = "";
    #endregion

    #region 批量移动---郑同飞
    public void moveUpdate(int resourceClassId)
    {
        try
        {
            string rid = Request.QueryString["rid"].ToString();
            if (rid == "")
            {
                Response.Write("1");
                Response.End();
            }
            
            if (rid.Contains(","))
            {
                string[] idParam = rid.Split(',');
                string[] ridParam = new string[idParam.Length];
                for (int i = 0; i < ridParam.Length; i++)
                {
                    ridParam[i] = idParam[i];
                }
                if (rBLL.Update(ridParam, resourceClassId))
                {

                }
            }
            else 
            {
                string[] resourceId ={ rid };
                if (rBLL.Update(resourceId,resourceClassId))
                {
                    
                }
            }
        }
        catch
        {
            
            
        }
    }
    #endregion
}
